package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import client.ClientConsole;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * Shows the login panel and connects the user the the server
 * @author Benny
 *
 */
public class LoginPanel extends JPanel {
	public JTextField textUserName;
	public JPasswordField textPassword;
	public MainFrame mf;
	public JButton btnLogin;
	public JLabel lblError;
	public ClientConsole client;
	BufferedImage myPicture = null;
	BufferedImage myPicture2 = null;
	/**
	 * Create the panel.
	 */
	public LoginPanel() {
		setPreferredSize(new Dimension(270, 199));
		setLayout(null);
		
		JLabel lblUsername = new JLabel("UserID");
		lblUsername.setBounds(27, 38, 72, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(27, 73, 72, 14);
		add(lblPassword);
		
		textUserName = new JTextField();
		textUserName.setBounds(96, 35, 133, 20);
		add(textUserName);
		textUserName.setColumns(10);
		
		textPassword = new JPasswordField();
		textPassword.setBounds(96, 70, 133, 20);
		add(textPassword);
		textPassword.setColumns(10);
		
		JLabel lblHead = new JLabel("Enter UserID and Password");
		lblHead.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblHead.setBounds(27, 12, 250, 14);
		add(lblHead);
		
		 btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				try{
					int userID=Integer.decode(textUserName.getText());
					ArrayList<Object> send = new ArrayList<>();
					send.add("Login");
					send.add(userID);
					send.add(textPassword.getText());
					mf.client.accept(send);
				}
				catch(NumberFormatException e)
				{
					loginFailed();
				}
				
			}
		});
		btnLogin.setBounds(20, 155, 89, 23);
		add(btnLogin);
		
		lblError = new JLabel("lblError");
		lblError.setVisible(false);
		lblError.setBounds(10, 102, 346, 41);
		add(lblError);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad.png"));
		
		JButton btnBookSearch = new JButton("Book Search");
		btnBookSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mf.userMain.bookSearch.init();
				mf.userMain.bookSearch.setVisible(true);
				setVisible(false);
				mf.userMain.bookSearch.btnNewButton.setVisible(true);
				
				for (int i = mf.librarianMain.searchBook.searchTableModel1.getRowCount() - 1; i >= 0; i--)
					 mf.librarianMain.searchBook.searchTableModel1.removeRow(i);
				for (int i = mf.librarianMain.searchBook.searchTableModel.getRowCount() - 1; i >= 0; i--)
					 mf.librarianMain.searchBook.searchTableModel.removeRow(i);
				for (int i = mf.userMain.bookSearch.searchTableModel1.getRowCount() - 1; i >= 0; i--)
					 mf.userMain.bookSearch.searchTableModel1.removeRow(i);
				for (int i = mf.userMain.bookSearch.searchTableModel.getRowCount() - 1; i >= 0; i--)
					 mf.userMain.bookSearch.searchTableModel.removeRow(i);
				
			}
		});
		btnBookSearch.setBounds(134, 155, 114, 23);
		add(btnBookSearch);
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(271, 199);
		add(picLabel);
		
	}
	/**
	 * sends the role of the specific user for showing the correct sidebar
	 * @param role specific user's role
	 */
	public void loginOK(String role){
		mf.showSideBar(role);
		mf.lblConnectedToServer.setVisible(false);
		btnLogin.setEnabled(false);
		setVisible(false);
	}
	/**
	 * Alerts the person that tries to login for incorrect information
	 */
	public void loginFailed(){
		lblError.setForeground(Color.RED);
		lblError.setText(" Could not log-in, Information incorrect");
		lblError.setVisible(true);
	}
}
